//
//  LabelExtension.swift
//  WineComing
//
//  Created by xiaozhi on 15/7/5.
//  Copyright (c) 2015年 xiaozhi. All rights reserved.
//

import UIKit
import CoreText
class LineLabel: UILabel {
    var lineView:UIView = UIView()
    func addLine()->Void{
        let aSize = CGSizeMake(self.getTextWidth().width, 1.0)
        var originX:CGFloat = 0.0
        switch self.textAlignment{
        case .Center:
            originX = (self.bounds.size.width-aSize.width)/2.0
        case .Right:
            originX = self.bounds.size.width - aSize.width
        default:break
        }
        lineView.frame = CGRectMake(originX, (self.bounds.size.height-aSize.height)/2.0, aSize.width, aSize.height)
        lineView.backgroundColor=self.textColor
        self.addSubview(lineView)
    }
}
extension UILabel{
    func getTextWidth()->CGSize{
        if let textStr = self.text{
            let attString = NSMutableAttributedString(string: textStr)
            attString.addAttribute(NSFontAttributeName, value: self.font, range: NSMakeRange(0, attString.length))
            let formatSetter = CTFramesetterCreateWithAttributedString(attString)
            var range = CFRangeMake(0, attString.length)
//            let path = CGPathCreateWithRect(CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height), nil)
//            let frameRef = CTFramesetterCreateFrame(formatSetter, CFRangeMake(0, attString.length), path, nil)
            let reallySize = CTFramesetterSuggestFrameSizeWithConstraints(formatSetter, CFRangeMake(0, attString.length), nil, CGSizeMake(bounds.size.width, 1000), &range)
            return reallySize
        }
        return CGSizeZero
    }
    func addColorWithreallySize(){
        let aSize = CGSizeMake(self.getTextWidth().width, 1.0)
        var originX:CGFloat = 0.0
        switch self.textAlignment{
        case .Center:
            originX = (self.bounds.size.width-aSize.width)/2.0
        case .Right:
            originX = self.bounds.size.width - aSize.width
        default:break
        }
        let aView = UIView(frame: CGRectMake(originX, (self.bounds.size.height-aSize.height)/2.0, aSize.width, aSize.height))
        aView.backgroundColor=self.textColor
        self.addSubview(aView)
    }
}